﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using  System.Windows;

namespace ProCP.Sim
{
    [Serializable()]
    class Car
    {
        [NonSerialized()]
        Brush brushCar = Brushes.Orange;
        public int[] destination;

        public RectangleF ToDraw ; //offset
        public RectangleF Buffer //insted of cage size of car ofseted per car towards $offset
        {
            get
            {
                PointF p = new PointF
                        (ToDraw.Location.X +ToDraw.Size.Width * offset.X, 
                        ToDraw.Location.Y + ToDraw.Size.Height * offset.Y);
                return new RectangleF((p),ToDraw.Size);
            }
        }
        bool driving;

        PointF moveToPoint;
        PointF offset;
        public int stage; // 0 then moving to enterence point 1 then moving to exit point
        
        public Car(Point possition,Size s,int[] destination) 
        {
            ToDraw = new  Rectangle(possition, s);
            moveToPoint = new Point();
            offset = new Point();
            stage = 1;
            this.destination = destination;
        }

        public bool MoveTo (Point p)
        {
            //float step = 15;
            if (!moveToPoint.Equals(p))
            {
                moveToPoint = p;
                //calculate offset
                offset.X = p.X - ToDraw.Location.X;
                offset.Y = p.Y - ToDraw.Location.Y;
                //normalization
                var length = (float)Math.Sqrt(offset.X*offset.X+offset.Y*offset.Y);
                if (offset.X != 0)
                {
                    offset.X =offset.X/length;
                    //TodoMultiply by $step
                }
                if (offset.Y != 0)
                {
                    offset.Y =offset.Y/length;
                    //TodoMultiply by $step
                }
            }
           //ToDraw.Location              
            if (Convert.ToInt32(ToDraw.Location.X)==moveToPoint.X&&
                Convert.ToInt32(ToDraw.Location.Y)==moveToPoint.Y) 
            { 
                return true;
            }
            ToDraw.Offset(offset);
            return false;
        }

        public void Draw(Graphics g) 
        {
            g.FillRectangle(brushCar, ToDraw);
        }
    }
}
